package com.iotechn.unimall.app.api.plantgoods;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.data.domain.PlantGoodsNeedDO;
import com.iotechn.unimall.data.enums.SystemStatusType;
import com.iotechn.unimall.data.mapper.PlantGoodsNeedMapper;
import com.iotechn.unimall.data.model.Page;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class PlantGoodsNeedServiceImpl implements PlantGoodsNeedService {

    @Autowired
    private PlantGoodsNeedMapper plantGoodsNeedMapper;


    @Override
    public Object selectGoodsByUser(Long userId) throws Exception {
        List<PlantGoodsNeedDO> plantGoodsDOs = plantGoodsNeedMapper.selectList(new EntityWrapper<PlantGoodsNeedDO>().eq("status", 0).eq("user_id", userId).orderBy("gmt_update", false));
        return plantGoodsDOs;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object insertGood(String title, Double stock, Long categoryId, String unit, String address, String street, Long areaId, String contactName, String contactPhone, Long userId) throws Exception {
        Date now = new Date();
        PlantGoodsNeedDO plantGoods = new PlantGoodsNeedDO();
        plantGoods.setTitle(title);
        plantGoods.setStock(stock);
        plantGoods.setCategoryId(categoryId);
        plantGoods.setUnit(unit);
        plantGoods.setStatus(SystemStatusType.ENABLE.getCode());
        plantGoods.setGmtUpdate(now);
        plantGoods.setGmtCreate(now);
        plantGoods.setAddress(address);
        plantGoods.setStreet(street);
        plantGoods.setStatus(0);
        plantGoods.setAreaId(areaId);
        /*if(areaId != null){
            AreaDO areaDO = areaMapper.selectById(areaId);
            if(areaDO != null){
                plantGoods.setArea(areaDO.getAreaName());
            }
        }*/
        plantGoods.setContactName(contactName);
        plantGoods.setContactPhone(contactPhone);
        plantGoods.setUserId(userId);
        return plantGoodsNeedMapper.insert(plantGoods);
    }

    public Object getGoodsById(Long goodsId) throws Exception {
        PlantGoodsNeedDO plantGoodsDO = plantGoodsNeedMapper.selectById(goodsId);
        return plantGoodsDO;

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object saveCurrPriceAndStock(Long goodId, Double currStock, String title, String contactName, String contactPhone, Long userId) throws Exception {
        Date now = new Date();
        PlantGoodsNeedDO plantGoodsDO = plantGoodsNeedMapper.selectById(goodId);
        Double stock = plantGoodsDO.getStock();
        plantGoodsDO.setTitle(title);
        plantGoodsDO.setStock(currStock);
        plantGoodsDO.setContactName(contactName);
        plantGoodsDO.setContactPhone(contactPhone);
        plantGoodsDO.setGmtUpdate(now);
        plantGoodsNeedMapper.updateById(plantGoodsDO);
        return plantGoodsDO;
    }

    public Object deleteByGoodId(
            Long goodId, Long userId
    ) throws Exception {
        PlantGoodsNeedDO plantGoodsDO = new PlantGoodsNeedDO();
        plantGoodsDO.setStatus(2);
        plantGoodsDO.setId(goodId);
        plantGoodsNeedMapper.updateById(plantGoodsDO);
        return plantGoodsDO;
    }

    /**
     * 按照时间倒序查询最新20条
     *
     * @return
     * @throws Exception
     */
    @Override
    public Object todayGoods() throws Exception {
        Wrapper<PlantGoodsNeedDO> wrapper = new EntityWrapper<PlantGoodsNeedDO>();
        wrapper.eq("status", 0);
        Integer page = 1;
        Integer limit = 20;
        //时间倒序
        wrapper.orderBy("gmt_create", false);
        Integer count = plantGoodsNeedMapper.selectCount(wrapper);
        List<PlantGoodsNeedDO> PlantGoodsDOS = plantGoodsNeedMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        return PlantGoodsDOS;//plantGoodsMapper.getTodayGoodsIndex();
    }

    @Override
    public Object getTodayGoods(Integer page, Integer limit, Long areaId, Long categoryId, String goodName, Double sPrice, Double ePrice) throws Exception {

        Wrapper<PlantGoodsNeedDO> wrapper = new EntityWrapper<PlantGoodsNeedDO>();
        wrapper.eq("status", 0);
        if (areaId != null) {
            wrapper.eq("area_id", areaId);
        }
        if (categoryId != null) {
            wrapper.eq("category_id", categoryId);
        }
        if (goodName != null) {
            wrapper.like("title", goodName);
        }
        if (sPrice != null && ePrice != null) {
            wrapper.between("price", sPrice, ePrice);
        }
        wrapper.orderBy("gmt_create", false);
        Integer count = plantGoodsNeedMapper.selectCount(wrapper);
        List<PlantGoodsNeedDO> PlantGoodsDOS = plantGoodsNeedMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        return new Page<>(PlantGoodsDOS, page, limit, count);
    }
}
